package com.htz.agency.controller;

import com.htz.agency.mapper.AnnouncementMapper;
import com.htz.agency.pojo.Announcement;
import com.htz.agency.pojo.query.AnnouncementQuery;
import com.htz.agency.pojo.vo.AnnouncementVO;
import com.htz.agency.service.AnnouncementService;
import com.htz.agency.util.PageResult;
import com.htz.agency.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * @author tang
 * @date 2025年06月27日 20:14
 */
@RestController
@RequestMapping("/announcement")
@Slf4j
//@Api(tags = "分类相关接口")
public class AnnouncementController {
    @Autowired
    private AnnouncementService announcementService;

    @Autowired
    private AnnouncementMapper announcementMapper;


    // 分页查询公告信息
    @GetMapping("/pageList")
    public PageResult<List<AnnouncementVO>> list(@RequestBody AnnouncementQuery query) {
        log.info("分页查询公告信息{}", query);
        log.info("查询参数{}", query.getTitle());
        PageResult<List<AnnouncementVO>>  list =  announcementService.list(query);
        return list;
    }

    // 根据Id查询公告信息
    @GetMapping("/{id}")
    public Result<AnnouncementVO> get(@PathVariable Long id) {
        log.info("查询id为{}的分类", id);
        AnnouncementVO byId1 = announcementMapper.getById(id);
        System.out.println(byId1.toString());
        Announcement byId = announcementService.getById(id);
        return Result.ok(byId);
    }

    // 保存公告信息
    @PostMapping("/save")
    public Result<String> save(@RequestBody Announcement announcement) {
        log.info("保存分类{}", announcement);
        announcement.setAdminId(1);
        boolean save = announcementService.save(announcement);
        if (save) {
            return Result.ok("保存成功");
        }
        return Result.error("保存失败");
    }

    // 修改公告信息
    @PutMapping("/update")
    public Result<String> update(@RequestBody Announcement announcement) {
        log.info("更新分类{}", announcement);
        boolean update = announcementService.updateById(announcement);
        if (update) {
            return Result.ok("更新成功");
        }
        return Result.error("更新失败");
    }

/*    //修改公告状态
    @PutMapping("/updateStatus")
    public Result<String> updateStatus(@RequestBody Announcement announcement) {
        log.info("获取状态{}", announcement.getIsActive());
        announcement.
        boolean update = announcementService.updateById(announcement);

    }*/

    //批量逻辑过期删除
    @DeleteMapping("/exDeleteAll")
    public Result exDeleteAll(Integer[] ids) {
        log.info("批量逻辑删除id{}", Arrays.stream(ids));
//        boolean delete = announcementService.removeByIds(Arrays.asList(ids));
        boolean delete = announcementService.exDeletedByIds(Arrays.asList(ids));
        if (delete) {
            return Result.ok("删除成功");
        }
        return Result.error("删除失败");
    }

    //定时逻辑删除批量删除
    @DeleteMapping("/deleteAll")
    public Result<String> deleteAll() {
        log.info("批量删除");
//        boolean delete = announcementService.removeByIds(Arrays.asList(ids));
        boolean delete = announcementService.deleteAll();
        if (delete) {
            return Result.ok("删除成功");
        }
        return Result.error("删除失败");
    }
    //定时过期批量删除
    @DeleteMapping("/deleteExAll")
    public Result<String> deleteExAll() {
        log.info("批量过期删除");
//        boolean delete = announcementService.removeByIds(Arrays.asList(ids));
        boolean delete = announcementService.expiredAnnouncement();
        if (delete) {
            return Result.ok("删除成功");
        }
        return Result.error("删除失败");
    }

}
